package com.cisc.controller;

import com.cisc.business.product.dto.ProductInfo;
import com.cisc.business.product.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @description: XXX
 * @author: daizh
 * @time: 2021/6/20 13:31
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping("/productId/{productId}")
    public ProductInfo getProductByProductId(@PathVariable("productId") Integer productId) {
        return productService.getProductByProductId(productId);
    }

    @GetMapping("/productCode/{productCode}")
    public ProductInfo getProductByProductCode(@PathVariable("productCode") String productCode) {
        return productService.getProductByProductCode(productCode);
    }

    @GetMapping("/allProdcut")
    public List<ProductInfo> getAllProducts() {
        return productService.getAllProducts();
    }

    @PostMapping("/saveProduct")
    public String saveProduct(@RequestBody ProductInfo productInfo) {
        try {
            productService.saveProduct(productInfo);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "fail";
        }
    }
}
